package com.derbysoft.dswitch.adapter.ihg.dswitch2.service.impl;

import com.derbysoft.dswitch.adapter.ihg.common.utils.DateTimeUtils;
import com.derbysoft.remote.dswitch.core.AbstractRQ;
import com.derbysoft.remote.dswitch.core.DestinationDTO;
import com.derbysoft.remote.dswitch.core.RequestHeader;
import com.derbysoft.remote.dswitch.core.SourceDTO;
import com.derbysoft.remote.dswitch.generic.constants.AgeQualifyingType;
import com.derbysoft.remote.dswitch.generic.constants.UniqueIDType;
import com.derbysoft.remote.dswitch.generic.dto.UniqueIDDTO;
import com.derbysoft.remote.dswitch.hotel.adapter.hotelprovider.HotelProviderWebService;
import com.derbysoft.remote.dswitch.hotel.adapter.hotelprovider.httpinvoker.HttpInvokerHotelProviderWebServiceFactory;
import com.derbysoft.remote.dswitch.hotel.dswitch.DSwitch4HotelBuyerWebService;
import com.derbysoft.remote.dswitch.hotel.dswitch.httpinvoker.HttpInvokerDSwitch4HotelBuyerWebServiceFactory;
import com.derbysoft.remote.dswitch.hotel.dto.GuestCountDTO;
import com.derbysoft.remote.dswitch.hotel.dto.StayDateRangeDTO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class IntegrationTestSupport {

    protected Log logger = LogFactory.getLog(getClass());

    private static final String CHECK_IN = "2013-02-15";
    private static final String CHECK_OUT = "2013-02-22";
    //private static final String CHANNEL_PASSPORT = "china";
    //private static final String CHANNEL_PASSPORT = "MMT";
    private static final String CHANNEL_PASSPORT = "DESPEGAR";
    protected static final String URL = "http://localhost:8010/dswitch2-ihg-adapter";
    //protected static final String URL = "http://54.248.100.20/dswitch3-ihg-adapter";
    //protected static final String URL = "http://175.41.251.53/dswitch2-ihg-adapter";
    //protected static final String URL = "http://10.200.107.35/dswitch2-ihg-adapter";
    //protected static final String HOTEL_PASSPORT = "IHG-SHGFD";
    protected static final String HOTEL_PASSPORT = "IHG-BUEEZ";
    protected static final String HOTEL_PASSPORTS = "HIHG022678,HIHG022660";

    //    protected static final String DSWITCH_URL = "http://114.142.151.226/dswitch2/";
    protected static final String DSWITCH_URL = "http://10.200.107.18/dswitch2/";


    protected void enhanceChannelRequestHeaderWithDestinations(AbstractRQ abstractRQ) {
        setChannelRquestHeader(abstractRQ);
        appendDestinations(abstractRQ);
    }

    protected StayDateRangeDTO createStayDateRange() {
        StayDateRangeDTO stayDateRange = new StayDateRangeDTO();
        stayDateRange.setCheckInDate(DateTimeUtils.parse(CHECK_IN));
        stayDateRange.setCheckOutDate(DateTimeUtils.parse(CHECK_OUT));
        return stayDateRange;
    }

    protected List<GuestCountDTO> createGuestCounts() {
        List<GuestCountDTO> guestCounts = new ArrayList<GuestCountDTO>();
        guestCounts.add(new GuestCountDTO(AgeQualifyingType.ADULT, 2));
        guestCounts.add(new GuestCountDTO(AgeQualifyingType.CHILD, 0));
        return guestCounts;
    }

    protected void setChannelRquestHeader(AbstractRQ abstractRQ) {
        RequestHeader header = new RequestHeader();
        header.setTaskId(UUID.randomUUID().toString());
        SourceDTO source = new SourceDTO();
        source.setUniqueID(createUniqueID(UniqueIDType.TRAVEL_AGENCY, CHANNEL_PASSPORT));
        //source.setUniqueID(createUniqueID(UniqueIDType.TRAVEL_AGENCY, "makemytrip"));
        header.setSource(source);
        ArrayList<UniqueIDDTO> products = new ArrayList<UniqueIDDTO>();
        UniqueIDDTO uniqueIDDTO = new UniqueIDDTO();
        uniqueIDDTO.setId(HOTEL_PASSPORT);
        uniqueIDDTO.setType(UniqueIDType.HOTEL);
        products.add(uniqueIDDTO);
        header.setProducts(products);
        abstractRQ.setHeader(header);
    }

    protected void appendDestinations(AbstractRQ abstractRQ) {
        RequestHeader header = abstractRQ.getHeader();
        List<DestinationDTO> destinations = new ArrayList<DestinationDTO>();
        DestinationDTO destination = new DestinationDTO();
        destination.setUniqueID(createUniqueID(UniqueIDType.HOTEL, "IHG"));
        destinations.add(destination);
        header.setDestinations(destinations);
        abstractRQ.setHeader(header);
    }

    protected UniqueIDDTO createUniqueID(UniqueIDType uniqueIDType, String uniqueIDValue) {
        UniqueIDDTO uniqueID = new UniqueIDDTO();
        uniqueID.setId(uniqueIDValue);
        uniqueID.setType(uniqueIDType);
        return uniqueID;
    }

    protected HotelProviderWebService getHotelProviderWebService() {
        HttpInvokerHotelProviderWebServiceFactory factory = new HttpInvokerHotelProviderWebServiceFactory(URL);
        return factory.getHotelProviderWebService();
    }


    protected DSwitch4HotelBuyerWebService getDswitchHotelProviderWebService() {
        HttpInvokerDSwitch4HotelBuyerWebServiceFactory factory = new HttpInvokerDSwitch4HotelBuyerWebServiceFactory(DSWITCH_URL);
        return factory.getDSwitch4HotelBuyerWebService();
    }

}
